import json
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import NoSuchElementException
import random
import time

# 设置 ChromeDriver 路径
chrome_driver_path = "/usr/local/bin/chromedriver"
# 配置 Chrome 选项
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(service=Service(executable_path=chrome_driver_path), options=options)

driver.get('https://movie.douban.com/top250?')


grid_view = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, 'grid_view'))
)

data_list=[]
for li in grid_view.find_elements(By.TAG_NAME, 'li'):
    hd = li.find_element(By.CLASS_NAME, 'hd')
    a = hd.find_element(By.TAG_NAME, 'a')
    href=a.get_attribute('href')

    titles = hd.find_elements(By.CLASS_NAME, 'title')
    other = hd.find_element(By.CLASS_NAME, 'other')
    inq= li.find_element(By.CLASS_NAME, 'inq')
    rating_num= li.find_element(By.CLASS_NAME, 'rating_num')
    print(f'ch: {a.text}, other: {other.text},rating_num:{rating_num.text} inq: {inq.text}', f'href:{href}')
    data={
        "title": a.text,
        "rating_num": rating_num.text,
        "inq": inq.text,
        "href": rating_num.text,
    }
    data_list.append(data)


# 将列表写入到txt文件中
    with open('output.txt', 'w', encoding='utf-8') as f:
        for data in data_list:
            f.write(f"href: {data['href']}\n")
            f.write(f"title: {''.join(data['title'])}\n")
            f.write(f"inq: {data['inq']}\n")
            f.write(f"rating_num: {data['rating_num']}\n")
            f.write('---\n')
driver.quit()

